package cn.itsource.pethome.system.role.controller;

import cn.itsource.pethome.system.role.service.IRoleService;
import cn.itsource.pethome.system.role.domain.Role;
import cn.itsource.pethome.system.role.query.RoleQuery;
import cn.itsource.pethome.utils.AjaxResult;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import javax.annotation.Resource;

@RestController
@RequestMapping("/role")
public class RoleController {
    @Resource
    public IRoleService roleService;


    /**
     * 保存和修改公用的
     * @param role  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Role role){
        try {
            if( role.getId()!=null){
                roleService.update(role);
                return AjaxResult.success().setMsg("修改成功!");
            }
            else{
                roleService.add(role);
                return AjaxResult.success().setMsg("添加成功！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error().setMsg(e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            roleService.delete(id);
            return AjaxResult.success().setMsg("删除成功");
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.error().setMsg(e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public Role get(@PathVariable("id")Long id)
    {
        return roleService.findOne(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<Role> list(){
        return roleService.findAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public AjaxResult json(@RequestBody RoleQuery query)
    {
        try {
            List<Role> query1 = roleService.findQuery(query);
            Long aLong = roleService.countPage(query);
            return AjaxResult.success().setData(query1).setTotal(aLong);
        }catch (Exception e){
            e.printStackTrace();
        }
        return AjaxResult.error();
    }
}
